package com.ocean.web.controller.index;

import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.common.utils.StringUtils;
import com.ocean.common.utils.spring.SpringUtils;
import com.ocean.product.service.IOProductService;
import com.ocean.system.service.ISysUserOnlineService;
import com.ocean.system.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.core.page.TableDataInfo;
import com.ocean.inxex.service.IndexService;
import com.ocean.inxex.vo.oNotice;
import com.ocean.productDict.domain.OHistoryDays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 前端页面跳转
 */
@Controller
@RequestMapping("/home")
public class IndexController {
    private String prefix = "index";

    @Autowired
    private IOProductService productService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 跳转到前端页面
     * @return
     */
    @GetMapping("")
    public String index()
    {
        return prefix + "/index";
    }

    /**
     * 跳转前端页面底部
     * @return
     */
    @GetMapping("/footer")
    public String footer()
    {
        return prefix + "/footer/footer";
    }

    /**
     * 跳转到home页面
     * @return
     */
    @GetMapping("/tohome")
    public String tohome()
    {
        return prefix + "/home/home";
    }

    /**
     * 获取首页通知数据
     * @param page
     * @return
     */
    @PostMapping("/notice")
    @ResponseBody
    public AjaxResult notice(int page)
    {
        List<oNotice> list = indexService.getNoticelist(page);
       return AjaxResult.success(list);
    }

    /**
     * 前端退出
     * @return
     */
    @PostMapping("/goout")
    @ResponseBody
    public AjaxResult goout()
    {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        SysUser user = ShiroUtils.getSysUser();
        if (StringUtils.isNotNull(user))
        {
            String loginName = user.getLoginName();
            // 清理缓存
            SpringUtils.getBean(ISysUserOnlineService.class).removeUserCache(loginName, ShiroUtils.getSessionId());
        }
        return AjaxResult.success(200);
    }

    @PostMapping("/getuser")
    @ResponseBody
    public AjaxResult getuser(@RequestParam(value = "loginname") String loginname)
    {
        SysUser sysUser = sysUserService.getloginName(loginname);
        return AjaxResult.success(sysUser);
    }

    @PostMapping("/upuser")
    @ResponseBody
    public AjaxResult upuser(String loginName,String phonenumber,String  email)
    {
        SysUser sysUser = sysUserService.getloginName(loginName);
        sysUser.setLoginName(loginName);
        sysUser.setPhonenumber(phonenumber);
        sysUser.setEmail(email);
        int i = sysUserService.updateUser(sysUser);
        return AjaxResult.success(i);
    }
}
